<?php
require_once("card.php");
require_once("cardtype.php");
class Deck {
	private $m_amountOfCards;
	private $m_cardsDealt;
	private $m_cards;
	
	function __construct($a_numberOfPlayers)
	{
		/* Link to document in testing purpose to calculate amount of cards! 
		 * https://docs.google.com/spreadsheet/ccc?key=0Aqjvd5dBUgxEdEQxbmZ4WUJFVUFrb2U1S2NaQXd0MWc&hl=en_US#gid=0
		*/
		$f_tempNumberOfPlayers = $a_numberOfPlayers;
		$move3 = 1*$f_tempNumberOfPlayers;
		$move2 = 2*$f_tempNumberOfPlayers;
		$move1 = 3*$f_tempNumberOfPlayers;
		$backUp = 1*$f_tempNumberOfPlayers;
		$turnLeftAndTurnRight = 3*$f_tempNumberOfPlayers;
		$uTurn = 1*$f_tempNumberOfPlayers;
		
		$this->m_amountOfCards = $move1+$move2+$move3+$backUp+$turnLeftAndTurnRight+$turnLeftAndTurnRight+$uTurn;
		
		$i = 0;

		while ($uTurn > 0) {
			$this->m_cards[$i] = new Card(CardType::uTurn, ($i + 1)*10);
			$i++;
			$uTurn--;
		}
		while($turnLeftAndTurnRight > 0){
			$this->m_cards[$i] = new Card(CardType::turnLeft, ($i +1)*10);
			$i++;
			$this->m_cards[$i] = new Card(CardType::turnRight, ($i +1)*10);
			$i++;
			$turnLeftAndTurnRight--;
		}
		while ($backUp > 0) {
			$this->m_cards[$i] = new Card(CardType::backUp, ($i + 1)*10);
			$i++;
			$backUp--;
		}
		while ($move1 > 0) {
			$this->m_cards[$i] = new Card(CardType::move1, ($i + 1)*10);
			$i++;
			$move1--;
		}
		while ($move2> 0) {
			$this->m_cards[$i] = new Card(CardType::move2, ($i + 1)*10);
			$i++;
			$move2--;
		}
		while ($move3 > 0) {
			$this->m_cards[$i] = new Card(CardType::move3, ($i + 1)*10);
			$i++;
			$move3--;
		}
		for ($i=0; $i < count($this->m_cards); $i++) { 
			echo $this->m_cards[$i]->GetType() . "  " . $this->m_cards[$i]->GetPriority() . " <br />";
		}
		$this->shuffleDeck();
		
	}
	
	function shuffleDeck()
	{
		$this->m_cardsDealt = 0;
		$i = 0;
		$j = 0;
		$f_lockedCards = array();
		while ($i < $this->m_amountOfCards) {
			$randomCardPos = rand(0, $this->m_amountOfCards-1);
			$this->swapCard($i, $randomCardPos);
			if ($this->m_cards[$i]->GetLockedStatus() === true) {
				array_push($f_lockedCards, $this->m_cards[$i]);
			}
			$i++;
		}
		
		while ($j < count($f_lockedCards)) {
			$this->swapCard($this->m_cards[$j], $f_lockedCards[$j]);
			$this->m_cardsDealt++;
			$j++;
		}
	}
	function dealCard()
	{
		return $this->m_cards[$this->m_cardsDealt++];
	}
	function swapCard($i, $pos)
	{
		$temp = $this->m_cards[$i];
		$temp2 = $this->m_cards[$pos];
		$this->m_cards[$i] = $temp2;
		$this->m_cards[$pos] = $temp;
	}
}
